﻿using System;
using System.Collections.Generic;

using System.Windows;
using System.Windows.Controls;

using System.Windows.Input;
using System.Windows.Navigation;

using EagleJournal.Components;

using EagleJournalBE.Util;
using EagleJournalBE.Database.Tables;
using System.ComponentModel;

namespace EagleJournal
{
	public partial class DialogPassword : UserControl
	{
        public static readonly  RoutedEvent
            InvokeDialogCloseEvent = EventManager.RegisterRoutedEvent("InvokeDialogClose", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(DialogPassword));
        public event            RoutedEventHandler
            InvokeDialogClose
        {
            add     { this.AddHandler(InvokeDialogCloseEvent, value); }
            remove  { this.RemoveHandler(InvokeDialogCloseEvent, value); }
        }

        public int      userType;

        bool            isInOptions = false;
        TableUserType   userTypes;

		public              DialogPassword                  ()
		{
			this.InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                userTypes = new TableUserType();
                userTypes.Refresh();
            }
		}

        private     void    ButtonClose_Click               (object sender, RoutedEventArgs e)
        {
            if (!isInOptions)
            {
                this.RaiseEvent(new RoutedEventArgs(InvokeDialogCloseEvent));
            }
            else
            {
                VisualStateManager.GoToState(this, "MainDialog", true);

                this.TextPword.Text = "Password options";
                isInOptions = false;
            }
        }

        private     void    ButtonForgot_Click              (object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "RecoverDialog", true);

            this.TextPword.Text = "Recover password";
            isInOptions = true;

            FocusHelper.Focus(this.TextRecover);
        }
        private     void    ButtonChange_Click              (object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "ChangeDialog", true);

            this.TextPword.Text = "Change password";
            isInOptions = true;

            FocusHelper.Focus(this.TextOld);
        }

        private     void    ButtonRecover_Click             (object sender, RoutedEventArgs e)
        {
            if (this.TextRecover.Text == "2000")
            {
                EagleMessageBox.ShowMessageBox("Recover password", "Your password is: " + (string)this.userTypes.TableData[userType - 1001][1] + "\n\nIt is advisable that you change your password after seeing this.", MessageBoxImage.Information, MessageBoxButton.OK);

                VisualStateManager.GoToState(this, "MainDialog", true);

                this.TextRecover.Text = string.Empty;

                this.TextPword.Text = "Password options";
                isInOptions = false;
            }
            else
            {
                EagleMessageBox.ShowMessageBox("Recover password", "Incorrect year entered.", MessageBoxImage.Error, MessageBoxButton.OK);
            }
        }
        private     void    ButtonReplace_Click             (object sender, RoutedEventArgs e)
        {
            string oldPassword = (string)this.userTypes.TableData[userType - 1001][1];

            if (oldPassword == this.TextOld.Password)
            {
                if (this.TextNew.Password == this.TextConfirm.Password)
                {
                    userTypes.Update(userType, new List<object>() { userType, this.TextNew.Password });

                    EagleMessageBox.ShowMessageBox("Password changed!", "Your password is now changed, click close so you can log in.", MessageBoxImage.Information, MessageBoxButton.OK);

                    VisualStateManager.GoToState(this, "MainDialog", true);

                    this.TextOld.Password       = string.Empty;
                    this.TextNew.Password       = string.Empty;
                    this.TextConfirm.Password   = string.Empty;

                    this.TextPword.Text = "Password options";
                    isInOptions = false;
                }
                else
                {
                    EagleMessageBox.ShowMessageBox("Wrong password", "The new passwords does not match!", MessageBoxImage.Error, MessageBoxButton.OK);
                }
            }
            else
            {
                EagleMessageBox.ShowMessageBox("Wrong password", "The password specified is not the same as the database has.", MessageBoxImage.Error, MessageBoxButton.OK);
            }
        }

        private     void    TextOld_PasswordChanged         (object sender, RoutedEventArgs e)
        {
            if (this.TextOld.Password.Length > 0)
            {
                this.TextNew.IsEnabled      = true;
                this.TextConfirm.IsEnabled  = false;
            }
            else
            {
                this.TextNew.IsEnabled      = false;
                this.TextNew.Password       = string.Empty;

                this.TextConfirm.IsEnabled  = false;
                this.TextConfirm.Password   = string.Empty;

                this.ButtonReplace.IsEnabled = false;
            }
        }
        private     void    TextNew_PasswordChanged         (object sender, RoutedEventArgs e)
        {
            if (this.TextNew.Password.Length > 0)
            {
                this.TextConfirm.IsEnabled = true;
            }
            else
            {
                this.TextConfirm.Password   = string.Empty;
                this.TextConfirm.IsEnabled  = false;

                this.ButtonReplace.IsEnabled = false;
            }
        }
        private     void    TextConfirm_PasswordChanged     (object sender, RoutedEventArgs e)
        {
            if (this.TextConfirm.Password.Length > 0)
            {
                this.ButtonReplace.IsEnabled = true;
            }
            else
            {
                this.ButtonReplace.IsEnabled = false;
            }
        }

        private     void    TextRecover_TextChanged         (object sender, TextChangedEventArgs e)
        {
            if (this.TextRecover.Text.Length > 0)
            {
                this.ButtonRecover.IsEnabled = true;
            }
            else
            {
                this.ButtonRecover.IsEnabled = false;
            }
        }
	}
}   
